package com.wwwarehouse.resource_center.fragment.createobject.modifydelete;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.wwwarehouse.common.bean.upload.FileUploadBean;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsSelectLabelBean;
import com.wwwarehouse.resource_center.bean.goods.ModifyDeleteBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.createobject.creategoods.CreateGoodsFragment;
import com.wwwarehouse.resource_center.fragment.createobject.createwarehouse.CreateWarehouseFragment;
import com.wwwarehouse.resource_center.utils.UploadPicUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Joseph on 2017/10/24.
 *         <p/>
 *         修改商品
 */

public class ModifyGoodsFragment extends CreateGoodsFragment {
    private ModifyDeleteBean.ListBean bean;
    private String mImgUrl;
    @Override
    public void setTitle() {
        if (peekFragment() instanceof ModifyGoodsFragment){
            mActivity.setTitle(getString(R.string.modify_goods));
        }
    }

    @Override
    protected void loadDatas() {
//        mGoodsName.setBackgroundColor(mActivity.getResources().getColor(R.color.white));
        img_name.setVisibility(View.GONE);
//        category_item.setVisibility(View.GONE);
//        mGoodsCategory.setVisibility(View.GONE);
        mGoodsAttribute.setSelected(true);
        mGoodsAttribute.setOnClickListener(this);
        mLLLaber.setVisibility(View.GONE);
        mGoodsPicture.setSelected(false);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mName.getLayoutParams();
        layoutParams.topMargin = 0;
        mName.setLayoutParams(layoutParams);
//        mName.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));

        Bundle bundle = getArguments();
        if (null != bundle) {
            bean = bundle.getParcelable("data");
            ownerUkid = bundle.getString("ownerUkid");
            pos = bundle.getInt("pos");
        }
        goodsName = bean.getName();
        mName.setText(bean.getName());
        goodsDes = bean.getIntroduction();
        mDes.setText(goodsDes);
        mTag.setVisibility(View.GONE);
        mLLBar.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(bean.getUnitCharacter() )){
            mUnitCharacter ="（"+ bean.getUnitCharacter()+"）";
        }
        mUnit.setText((bean.getUnitName() + mUnitCharacter));
        unitUkid = (bean.getUnitUkid());
        abstractObjectUkid = bean.getAbstractObjectUkid();
        try {
            mCategory.setText(bean.getCategoryList().get(0).getName());
            categoryUkid = (bean.getCategoryList().get(0).getCategoryUkid());
            metaCategoryUkid = (bean.getCategoryList().get(0).getMetaCategoryUkid());
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            mImgUrl = bean.getThumbnail();
            fileUploadBean = new FileUploadBean();
            List<FileUploadBean.DataBean> dataBeen = new ArrayList<>();
            FileUploadBean.DataBean dataBean = new FileUploadBean.DataBean();
            List<ModifyDeleteBean.ListBean.MediaListBean.SubMediasBean> subMedias = bean.getMediaList().get(0).getSubMedias();
            if (subMedias!=null&&subMedias.size()>0){
                for (int i = 0; i < subMedias.size(); i++) {
                    if (subMedias.get(i).getSubMediaType().equals("MEDIUM_IMAGE")){
                        dataBean.setName(bean.getMediaList().get(0).getMediaName());
                        dataBean.setPath(subMedias.get(i).getSubMediaUrl());
                        dataBeen.add(dataBean);
                        fileUploadBean.setData(dataBeen);
                        break;
                    }
                }
            }else {
                dataBean.setName(bean.getMediaList().get(0).getMediaName());
                dataBean.setPath(bean.getMediaList().get(0).getRelatedUrl());
                dataBeen.add(dataBean);
                fileUploadBean.setData(dataBeen);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (TextUtils.isEmpty(mImgUrl)) {
            mGoodsPicture.setSelected(false);
            mLLPic.setVisibility(View.VISIBLE);
            img_descrip.setText("暂无图片");
            no_pic.setBackgroundResource(R.drawable.picture_no);
            img_descrip.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
        } else {
            mLLPic.setVisibility(View.GONE);
            mIvPic.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(mImgUrl, mIvPic, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {

                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    mGoodsPicture.setSelected(false);
                    mLLPic.setVisibility(View.VISIBLE);
                    img_descrip.setText("暂无图片");
//                                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) img_descrip.getLayoutParams();
//                                    layoutParams.topMargin = 100;
//                                    img_descrip.setLayoutParams(layoutParams);
                    no_pic.setBackgroundResource(R.drawable.picture_no);
                    img_descrip.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                }

                @Override
                public void onLoadingCancelled(String s, View view) {

                }
            });
        }
        try {
            List<ModifyDeleteBean.ListBean.IdentifyListBean> identifyList = bean.getIdentifyList();
            if (identifyList!=null){
                for (int i = 0; i < identifyList.size(); i++) {
                     if (identifyList.get(i).getIdentifyName().equals("0")){
                        mBarCode.setText(identifyList.get(i).getIdentifyCode());
                    }
                    if (identifyList.get(i).getIdentifyName().equals("4")){
                        mSequenceCode.setText(identifyList.get(i).getIdentifyCode());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ModifyDeleteBean.ListBean.LabelListBean> labelList = bean.getLabelList();
        String jsonString = JSON.toJSONString(labelList);
        data = (ArrayList<GoodsSelectLabelBean.ListBean>) JSON.parseArray(jsonString, GoodsSelectLabelBean.ListBean.class);
        if (data != null)
            setLaber(data);
        List<ModifyDeleteBean.ListBean.AttributeListBean> attributeList = bean.getAttributeList();
        listBeen = (ArrayList<GoodsAttributesBean.ListBean>) JSON.parseArray(JSON.toJSONString(attributeList), GoodsAttributesBean.ListBean.class);
        setAttributes(listBeen);
    }

    @Override
    public void titleBt1Click() {
        if (peekFragment() instanceof ModifyGoodsFragment) {
            if (path != null && !path.isEmpty()) {
                showProgressDialog("图片上传中...");
                UploadPicUtil.getInstance().uploadPic(path, mFile, getActivity());
            } else {
                request(ResourceConstant.UPDATE_ABSTRACT_OBJECT, "UPDATE_H_GOODS");
            }
        }
    }
}
